package com.matrix.kernel.util;


import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import com.matrix.kernel.xss.SQLFilter;

/**
 * 查询参数
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-03-14 23:15
 */
public class Query extends LinkedHashMap<String, Object> {
    private static final long serialVersionUID = 1L;
    //当前页码
    private int page;
    //每页条数
    private int limit;

    public Query(Map<String, Object> params) {
        Map<String, Object> m = new HashMap<>();
        for (String s : params.keySet()) {
            Object o = params.get(s);
            if (o != null && o instanceof String) {
                o = ((String) o).trim();
            }
            String key = s.toLowerCase();
            if(o != null && !"".equals(o) &&   (key.indexOf("enddate") != -1 || key.indexOf("endcreatedate") != -1 || key.indexOf("enddeaddate") != -1
                    || key.indexOf("endreceivedate") != -1 || key.indexOf("endconfirmdate") != -1)){
                String endDate = o + " " + "23:59:59";
                m.put(s, endDate);
            }else{
                m.put(s, o);
            }

        }
        this.putAll(m);

        //分页参数
        this.page = Integer.parseInt(m.get("page").toString());
        this.limit = Integer.parseInt(m.get("limit").toString());
        this.put("offset", (page - 1) * limit);
        this.put("page", page);
        this.put("limit", limit);

        //防止SQL注入（因为sidx、order是通过拼接SQL实现排序的，会有SQL注入风险）
//        String sidx = m.get("sidx").toString();
//        String order = m.get("order").toString();
//        this.put("sidx", SQLFilter.sqlInject(sidx));
//        this.put("order", SQLFilter.sqlInject(order));
    }


    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        this.limit = limit;
    }
}
